import NodeUtil from "../Framework/Utils/NodeUtil";
import { ZTool } from "../Framework/Utils/ZTool";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ValueBar extends cc.Component {

    ValueBarFG: cc.Sprite = null;
    TitleLabel: cc.Label = null;

    protected onLoad(): void {
        this.ValueBarFG = NodeUtil.GetComponent("ValueBarFG", this.node, cc.Sprite);
        this.TitleLabel = NodeUtil.GetComponent("TitleLabel", this.node, cc.Label);
    }

    Init(value: number = 1, name: string = ``, color: cc.Color = cc.Color.WHITE) {
        this.ValueBarFG = NodeUtil.GetComponent("ValueBarFG", this.node, cc.Sprite);
        this.TitleLabel = NodeUtil.GetComponent("TitleLabel", this.node, cc.Label);
        this.ValueBarFG.node.color = color;
        this.TitleLabel.string = name;

        this.Set(value);
    }

    Set(rate: number) {
        rate = ZTool.Clamp(rate, 0, 1);
        this.ValueBarFG.fillRange = rate;
        if (this.TitleLabel.string == "耐久度") {
            this.ValueBarFG.node.color = cc.color(255, 255 * rate, 255 * rate, 255);
        }
    }

    SetAll(rate: number, name: string = ``) {
        rate = ZTool.Clamp(rate, 0, 1);
        this.ValueBarFG.fillRange = rate;
        this.TitleLabel.string = name;
        if (this.TitleLabel.string == "耐久度") {
            this.ValueBarFG.node.color = cc.color(255, 255 * rate, 255 * rate, 255);
        }
    }
}
